<?php

namespace Home\Controller;

use Think\Controller;
use Home\Controller\CaptchaController;
use Think\Crypt;

class MemberController extends Controller {

    public $userInfoModel = null;
    public $userModel = null;
    public $mem = null;
    private $userinfo;

    public function __construct() {
        parent::__construct();
        $this->userInfoModel = new \Home\Model\UserInfoModel();
        $this->userModel = new \Home\Model\UserModel();
        //var_dump($this->userModel->check_login());exit;
        if ($this->userModel->check_login() < 0) {
            $this->redirect("Index/index");
        }
    }

    public function index() {
        session('[start]');
        $this->userinfo = session('userinfo');

        $last_game_model = M('game_user_relation');
        $sql = "select g.gamename,g.enname,g.main_site,gc.name from game_cate as gc,game_list as g,game_user_relation as gur where gur.uid='" . $this->userinfo['uid'] . "' and gur.game_id=g.id and g.cateid=gc.cid";
        $last_game = $last_game_model->query($sql);

        if ($this->userinfo['email'] != '') {
            //邮箱已设置
            $data['e_tag'] = "已设置";
            $data['e_style_1'] = "safety_ok";
            $data['e_style_2'] = "safety_s2";
        } else {
            //邮箱未设置
            $data['e_tag'] = "未设置";
            $data['e_style_1'] = "safety_no";
            $data['e_style_2'] = "safety_s4";
        }
        //根据 uid查看身份证信息是否已经设置
        $userModel = M("user_add_info");
        $user = $userModel->where("uid='%d'", $this->userinfo['uid'])->find();
        if (!empty($user['real_name']) && !empty($user['idcard'])) {
            $data1['e_tag'] = "已设置";
            $data1['e_style_1'] = "safety_ok";
            $data1['e_style_2'] = "safety_s2";
        } else {
            $data1['e_tag'] = "未设置";
            $data1['e_style_1'] = "safety_no";
            $data1['e_style_2'] = "safety_s4";
        }
        $this->assign('last_game', $last_game);
        $this->assign('data', $data);
        $this->assign('data1', $data1);
        $this->assign('on2a', 'on2a');
        $this->display();
    }

    public function setemail() {
        session("[start]");
        $this->userinfo = session('userinfo');
        if (IS_AJAX) {
            $email = I("get.email");
            if (!$this->userInfoModel->check_email($email)) {
                $this->ajaxReturn(array('ret' => 7, 'msg' => '邮箱格式错误'), 'json');
            }
            $captcha = I("get.captcha");
            $verify = new CaptchaController();
            if (!$verify->check_verify($captcha)) {
                $this->ajaxReturn(array('ret' => 5, 'msg' => '验证码错误'), 'json');
            }
            //$token = md5($this->userinfo['uid'] . $this->userinfo['username'] . $email);
            if ($this->userInfoModel->addEmail($this->userinfo['uid'], $email)) {
                //发一封激活邮件
                //TODO
                //$flag = $this->sendEmail($email);
                $_SESSION['userinfo']['email'] = $email;
                $this->ajaxReturn(array('ret' => 10, 'msg' => '邮箱添加成功！'), 'json');
            } else {
                $this->ajaxReturn(array('ret' => 9, 'msg' => '邮箱添加失败，请稍后在试'), 'json');
            }
        }
        $email = $this->userinfo['email'] ? $this->replace_mask($this->userinfo['email']) : '';
        $this->assign('on2a', 'on2a');
        $this->assign('email', $email);
        $this->display();
    }

    /**
     * 邮箱添加成功跳转
     */
    public function success_email() {
        $emial = $this->replace_mask($_SESSION['userinfo']['email']);
        $this->assign('email', $emial);
        $this->display();
    }

    public function authentication() {
        session('[start]');
        $data['real_name'] = $_SESSION['userinfo']['real_name'] ? $_SESSION['userinfo']['real_name'] : '';
        $data['idcard'] = $_SESSION['userinfo']['idcard'] ? $_SESSION['userinfo']['idcard'] : '';
        $this->assign('data', $data);
        $this->assign('on2a', 'on2a');
        $this->display();
    }

    /**
     * 修改密码
     */
    public function passport() {
        $this->display();
    }

    /**
     * 字符串替换  steven@qq.com 替换成 ste***@qq.com
     * @param type $mail
     * @return type
     */
    private function replace_mask($mail) {
        $email = $mail;
        $val = strpos($email, '@') / 2;
        $replace = '';
        for ($i = 0; $i < $val; $i++) {
            $replace.='*';
        }
        $str = substr($email, $val, $val);
        $output = str_replace($str, $replace, $email);
        return $output;
    }

    public function checkusername() {
        $username = I('get.username');
        if ($this->userModel->getByUsername($username)) {
            $this->ajaxReturn(array('ret' => 0, 'msg' => '验证成功'));
        } else {
            $this->ajaxReturn(array('ret' => 1, 'msg' => '用户名不存在'));
        }
    }

    public function checkpass() {
        session('[start]');
        $oldpassword = I('get.oldpassword');
        $this->userinfo = $_SESSION['userinfo'];

        if (!Crypt::Crypt_Hash_Compare($this->userinfo['password'], $oldpassword)) {
            $this->ajaxReturn(array('ret' => 1, 'msg' => '密码验证失败'));
        } else {
            $this->ajaxReturn(array('ret' => 0, 'msg' => '验证成功'));
        }
    }

    function upass() {
        $username = T('post.username');
        $oldpassword = I('post.oldpassword');
        $newpassword = I('post.newpassword');
        $renewpassword = I('post.renewpassword');
        $captcha = I('post.captcha');
        session('[start]');
        $this->userinfo = $_SESSION['userinfo'];

        if (!Crypt::Crypt_Hash_Compare($this->userinfo['password'], $oldpassword)) {
            $this->ajaxReturn(array('ret' => 1, 'msg' => '密码验证失败'));
        }

        if (!$this->userModel->check_password($newpassword)) {
            $this->ajaxReturn(array('ret' => 2, 'msg' => '6~12个字符，只允许数字、字母、下划线'), 'json');
        }

        if (!$this->userModel->compare_password($newpassword, $renewpassword)) {
            $this->ajaxReturn(array('ret' => 3, 'msg' => '两次密码输入不一样'), 'json');
        }

        $verify = new CaptchaController();
        if (!$verify->check_verify($captcha)) {
            $this->ajaxReturn(array('ret' => 5, 'msg' => '验证码错误'), 'json');
        }

        if ($this->userModel->update_password($this->userinfo, Crypt::Crypt_Hash_Create($newpassword))) {
            $this->ajaxReturn(array('ret' => 0, 'msg' => '密码修改成功！'));
        } else {
            $this->ajaxReturn(array('ret' => 1, 'msg' => '密码修改失败！'));
        }
    }

}
